package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Performance;
import com.itheima.pojo.Result;
import com.itheima.service.PerformanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/performance")
public class PerformanceController {
    @Autowired
    private PerformanceService performanceService;

    @PostMapping("/add")
    public Result add(@RequestBody Performance performance) {
        performanceService.save(performance);
        return Result.success("成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        performanceService.removeById(id);
        return Result.success("成功");
    }

    @PutMapping("/put/{id}")
    public Result update(@PathVariable Long id, @RequestBody Performance performance) {
        // 校验 ID 是否匹配
        if (id == null) {
            return Result.error("请求的ID与数据中ID不一致");
        }

        // 更新操作
        boolean isUpdated = performanceService.updateById(performance);

        if (isUpdated) {
            return Result.success("成功");
        } else {
            return Result.error("更新失败");
        }
    }

    // 获取绩效信息
    @GetMapping("/detail/{id}")
    public Result getPerformanceById(@PathVariable Long id) {
        // 根据 ID 查询绩效信息
        Performance performance = performanceService.getById(id);

        // 如果没有找到对应的绩效信息，返回错误信息
        if (performance == null) {
            return Result.error("未找到该绩效信息");
        }

        // 返回成功的响应，包含绩效信息
        return Result.success(performance);
    }

    @GetMapping("/list")
    public Result<Page> page(Integer page, Integer pagesize, String employeeName,Integer employeeId,String period,String level ) {
        Page page1 = new Page(page, pagesize);
        return performanceService.selectPage(page1, employeeName,employeeId,period,level);
    }

}
